package com.plsjavaweb.dao.mapper;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.SearchPOJO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;


public class SearchMapper implements Mapper<SearchPOJO> {

	Logger log= Logger.getLogger(DAOFactory.class);
	LoggingConstants logConst = new LoggingConstants();
	
	@Override
	public List<SearchPOJO> MapList(ResultSet rs) throws SQLException {
		log.info(logConst.entryMessage);
		
		List<SearchPOJO> searchResultList = null;
		SearchPOJO searchResult = null;
		try {			
			if (rs == null){					
			} else {
				
				searchResultList = new ArrayList<>();
				while(rs.next()){
					searchResult = new SearchPOJO();
					searchResult.setFirstname(rs.getString("first_name"));
					searchResult.setLastname(rs.getString("last_name"));
					searchResult.setLocalNo(Integer.parseInt(rs.getString("local_no")));
					searchResult.setSeatCode(rs.getString("seat_code"));
					searchResultList.add(searchResult);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			log.info(logConst.errorMessage);
			
		}
		
		log.info(logConst.returnedMessage);	
		return searchResultList;
	}

	@Override
	public SearchPOJO MapObject(ResultSet rs) throws SQLException {
		log.info(logConst.entryMessage);
		
		SearchPOJO searchResult = null;		
		try {			
			if (rs != null) {
				searchResult = new SearchPOJO();
				while(rs.next())
				{
					searchResult.setFirstname(rs.getString("first_name"));
					searchResult.setLastname(rs.getString("last_name"));
					searchResult.setLocalNo(Integer.parseInt(rs.getString("local_no")));
					searchResult.setSeatCode(rs.getString("seat_code"));
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			log.info(logConst.errorMessage);
		}			
		log.info(logConst.returnedMessage);	
		return searchResult;
	}
}
